package com.cnswhy.cloud.server.comsumer.consumer;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.BeanPlusUtil;
import com.cnswhy.cloud.common.core.utils.SystemClockUtil;
import com.cnswhy.cloud.common.logging.uitls.MDCUtil;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.WithdrawType;
import com.cnswhy.cloud.server.common.entity.mq.WithdrawRecordMQ;
import com.cnswhy.cloud.server.common.entity.mq.WithdrawVerifyMQ;
import com.cnswhy.cloud.server.comsumer.event.WithdrawRecordMqEvent;
import com.cnswhy.cloud.server.core.entity.*;
import com.cnswhy.cloud.server.core.service.CustomerWechatService;
import com.cnswhy.cloud.server.core.service.CustomerWithdrawalRecordService;
import com.cnswhy.cloud.server.core.service.customer.CustomerAlipayService;
import com.cnswhy.cloud.server.core.service.customer.CustomerBankService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.trans.WithdrawalRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@Slf4j
public class WithdrawRecordMqConsumer implements WithdrawRecordMqEvent {
    @Autowired
    CustomerService customerService;
    @Autowired
    WithdrawalRecordService withdrawalRecordService;
    @Autowired
    CustomerBankService customerBankService;
    @Autowired
    CustomerWithdrawalRecordService customerWithdrawalRecordService;
    @Autowired
    CustomerAlipayService customerAlipayService;
    @Autowired
    private CustomerWechatService customerWechatService;
    @Autowired
    MessageService messageService;

    @Override
    public void createVerifyRecord(WithdrawRecordMQ withdrawRecordMQ) {
        WithdrawalRecord withdrawalRecord = withdrawalRecordService.getById(withdrawRecordMQ.getId());
        log.info("获取record:{}", JSON.toJSONString(withdrawalRecord));
        if(withdrawalRecord == null){
            return;
        }
        CustomerWithdrawalRecord customerWithdrawalRecord = save(withdrawalRecord);
        verify(customerWithdrawalRecord);
        WithdrawVerifyMQ withdrawVerifyMQ = WithdrawVerifyMQ.builder().id(customerWithdrawalRecord.getId()).build();
        withdrawVerifyMQ.setTraceId(MDCUtil.getTraceId());
        messageService.sendQueueByObject(AmqpExchange.WITHDRAW_VERIFY_MESSAGE, withdrawVerifyMQ);
    }
    public void verify(CustomerWithdrawalRecord customerWithdrawalRecord){
        customerWithdrawalRecord.setVerifyStatus(2);
        customerWithdrawalRecord.setVerifyTime(SystemClockUtil.getLocalDateTime());
        customerWithdrawalRecordService.updateById(customerWithdrawalRecord);

    }
    public  CustomerWithdrawalRecord save(WithdrawalRecord withdrawalRecord){
        CustomerWithdrawalRecord customerWithdrawalRecord = null;
        customerWithdrawalRecord = customerWithdrawalRecordService.getOne(Wraps.<CustomerWithdrawalRecord>lbQ().eq(CustomerWithdrawalRecord::getOrderId,withdrawalRecord.getOrderNum()));
        if(customerWithdrawalRecord != null){
            return null;
        }
        customerWithdrawalRecord = new CustomerWithdrawalRecord();
        customerWithdrawalRecord.setCustomerId(withdrawalRecord.getCustomerId());
        customerWithdrawalRecord.setTransAmount(Long.valueOf(withdrawalRecord.getRealPay().multiply(BigDecimal.valueOf(100.0)).longValue() ));
        customerWithdrawalRecord.setSettAmt(Long.valueOf(withdrawalRecord.getAmount().multiply(BigDecimal.valueOf(100.0)).longValue() ));
        customerWithdrawalRecord.setTransFee(customerWithdrawalRecord.getSettAmt() - customerWithdrawalRecord.getTransAmount());
        customerWithdrawalRecord.setVerifyStatus(1);
        customerWithdrawalRecord.setVerifyStatus(1);
        customerWithdrawalRecord.setSettDate(SystemClockUtil.getSettDateyyyyMMdd());
        customerWithdrawalRecord.setOrderId(withdrawalRecord.getOrderNum());
        customerWithdrawalRecord.setBalanceType(withdrawalRecord.getType());
        Customer customer = customerService.getById(withdrawalRecord.getCustomerId());
        log.info("查询用户:{}", JSON.toJSONString(customer));
        customerWithdrawalRecord.setLoginName(customer.getLoginName());
        customerWithdrawalRecord.setBalanceType(withdrawalRecord.getType());
        if(withdrawalRecord.getType() == 2){
            customerWithdrawalRecord.setWithdrawType(WithdrawType.ALIPAY);
            CustomerAlipay customerAlipay = customerAlipayService.getById(withdrawalRecord.getBankId());
            log.info("查询用户支付宝:{}", JSON.toJSONString(customerAlipay));
            customerWithdrawalRecord.setRealName(customerAlipay.getRealName());
            customerWithdrawalRecord.setMobile(customerAlipay.getMobile());
            customerWithdrawalRecord.setWithdrawAccount(customerAlipay.getAliAccount());
        } else if (withdrawalRecord.getType() == 3) {
            customerWithdrawalRecord.setWithdrawType(WithdrawType.WxPAY);
            CustomerWechat customerWechat = customerWechatService.getById(withdrawalRecord.getBankId());
            log.info("查询用户微信信息:{}", JSON.toJSONString(customerWechat));
            customerWithdrawalRecord.setWithdrawAccount(customerWechat.getNickName());
            customerWithdrawalRecord.setRealName(customerWechat.getRealName());
            customerWithdrawalRecord.setMobile(customerWechat.getMobile());
            customerWithdrawalRecord.setIdCard(customerWechat.getOpenId());
        }else{
            customerWithdrawalRecord.setWithdrawType(WithdrawType.BANKCARD);
            CustomerBank customerBank = customerBankService.getById(withdrawalRecord.getBankId());
            log.info("查询用户银行信息:{}", JSON.toJSONString(customerBank));
            customerWithdrawalRecord.setWithdrawAccount(customerBank.getBankNum());
            customerWithdrawalRecord.setRealName(customerBank.getAccountName());
            customerWithdrawalRecord.setBankBranch(customerBank.getBankBranch());
            customerWithdrawalRecord.setMobile(customerBank.getMobile());
            customerWithdrawalRecord.setIdCard(customerBank.getIdCard());
        }
        customerWithdrawalRecordService.save(customerWithdrawalRecord);
        log.info("保存审核记录:{}",JSON.toJSONString(customerWithdrawalRecord));
        return customerWithdrawalRecord;
    }
}
